package com.southwind.controller;

import com.southwind.form.RegisterForm;
import com.southwind.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @GetMapping("/register")
    public String showRegisterPage() {
        return "register"; // 返回 register.html 页面
    }

    @PostMapping("/register")
    public String handleRegister(RegisterForm registerForm, Model model) {
        boolean success = registerService.register(registerForm);
        if (!success) {
            model.addAttribute("error", "该手机号已被注册");
            return "register";
        }
        return "redirect:/login?registered";
    }
}
